package com.liyl.util;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.ExcelBuilderImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.CollectionUtils;
import org.thymeleaf.expression.Lists;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 80005054
 */
@Slf4j
public class ExportUtil {

    public static <T> List<List<Object>> getExportListDatas(List<T> list, List<String> includeColumns) {
        List<List<Object>> listData = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return listData;
        }
        try {
            for (T t : list) {
                List<Object> rowLine = new ArrayList<>();
                for (String s : includeColumns) {
                    String getMethodName = "get" + s.substring(0, 1).toUpperCase() + s.substring(1);
                    Class clazz = t.getClass();
                    Method getMethod;
                    getMethod = clazz.getMethod(getMethodName, new Class[]{});
                    Object value = getMethod.invoke(t, new Object[]{});
                    rowLine.add(value);
                }
                listData.add(rowLine);
            }
        } catch (Exception e) {
            log.error("生成EXCEL错误：{}", e.getMessage());
        }
        return listData;
    }

    public static List<String> fcstComparisonVoColumnsByOrder() {
        return Arrays.asList("materialGroup", "gapSupplyM0");
    }

    /**
     * **获取workbook**
     * 因为EasyExcel这个库设计的原因
     * 只能使用反射获取workbook
     *
     * @param writer
     * @return
     */
    private static Workbook getWorkbook(ExcelWriter writer) {
        Workbook workbook = null;
        try {
            Class<?> clazz1 = Class.forName("com.alibaba.excel.ExcelWriter");
            Field[] fs = clazz1.getDeclaredFields();
            for (Field field : fs) {
                // 要设置属性可达，不然会抛出IllegalAccessException异常
                field.setAccessible(true);
                if ("excelBuilder".equals(field.getName())) {
                    ExcelBuilderImpl excelBuilder = (ExcelBuilderImpl) field.get(writer);
                    Class<?> clazz2 = Class.forName("com.alibaba.excel.write.ExcelBuilderImpl");
                    Field[] fs2 = clazz2.getDeclaredFields();
                    for (Field field2 : fs2) {
                        field2.setAccessible(true);
                        if ("context".equals(field2.getName())) {
                            WriteContext context = (WriteContext) field2.get(excelBuilder);
                            workbook = context.writeWorkbookHolder().getWorkbook();
                        }
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return workbook;
    }
}
